local yanshou = fk.CreateSkill{
  name = "rfenghou_3d18j__yanshou",
  max_phase_use_time = function(self, player)
    local slash = Fk:cloneCard("slash")
    return slash.skill:getMaxUseTime(player, Player.HistoryPhase, slash) - player:usedCardTimes("slash", Player.HistoryPhase) + player:usedSkillTimes(self.name, Player.HistoryPhase)
  end,
  dynamic_desc = function(self, player)
    local mark = player:getMark("@rfenghou_3d18j__yanshou")
    local slash = Fk:cloneCard("slash")
    return "rfenghou_3d18j__yanshou-dynamic:" .. 3 - mark .. ":" .. slash.skill:getMaxUseTime(player, Player.HistoryPhase, slash)
  end
}

yanshou:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@rfenghou_3d18j__yanshou", 0)
end)

yanshou:addEffect("active", {
  anim_type = "drawcard",
  times = function(self, player)
    return player.phase == Player.Play and yanshou:getRemainUseTime(player, Player.HistoryPhase) or -1
  end,
  prompt = function(self, player)
    local prompt = Fk:translate("#rfenghou_3d18j__yanshou")
    if yanshou:getRemainUseTime(player, Player.HistoryPhase) == 1 then
      prompt = prompt .. Fk:translate("#rfenghou_3d18j__yanshou-last")
    end
    return prompt
  end,
  can_use = function(self, player)
    return player:canUseTo(Fk:cloneCard("await_exhausted"), player)
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  feasible = function(self, player, selected, selected_cards)
    local c = Fk:cloneCard("await_exhausted")
    c.skillName = yanshou.name
    return not player:isProhibited(player, c) and
        Util.CardTargetFilter(c.skill, player, player, {}, {}, c)
  end,
  target_filter = Util.FalseFunc,
  on_cost = function(self, player, data, extra_data)
    if yanshou:getRemainUseTime(player, Player.HistoryPhase) == 1 then
      return {
        extra_data = {
          rfenghou_3d18j__yanshouLast = true
        }
      }
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    player:addCardUseHistory("slash")
    local c = Fk:cloneCard("await_exhausted")
    c.skillName = yanshou.name
    local use = {
      from = player,
      tos = { player },
      card = c,
      extra_data = effect.extra_data
    }
    room:useCard(use)
  end,
}, { check_skill_limit = true })
yanshou:addEffect("targetmod", {
  fix_times_func = function(self, player, skill, scope, card, to)
    if card and card.trueName == "slash" and scope == Player.HistoryPhase and player:hasSkill(yanshou.name) then
      return 3 - player:getMark("@rfenghou_3d18j__yanshou")
    end
  end
})
yanshou:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true, 3)
    if use_event then
      local use = use_event.data
      if use.from == player and use.card.trueName == "await_exhausted" and table.contains(use.card.skillNames, yanshou.name) and use.extra_data and use.extra_data.rfenghou_3d18j__yanshouLast then
        local cards = {}
        for _, move in ipairs(data) do
          if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard
          -- 别乱写move.skillName求你们了
            and type(move.skillName) == "string" and string.find(move.skillName, "await_exhausted") then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(player.room.discard_pile, info.cardId) then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
        if #cards > 0 then
          cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
          if #cards > 0 then
            event:setCostData(self, {cards = cards, event = use_event})
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local cards, event = dat.cards, dat.event
    event:addExitFunc(function()
      room:obtainCard(player, cards, nil, fk.ReasonPrey)
      room:addPlayerMark(player, "@rfenghou_3d18j__yanshou")
    end)
  end,
})
yanshou:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and data.card and data.card.trueName == "slash" then
      return not data.extraUse and player:getMark("@rfenghou_3d18j__yanshou") == 2
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.disresponsiveList = room:getAlivePlayers(false)
    data.additionalDamage = (data.additionalDamage or 0) + 1
    room:setPlayerMark(player, "@rfenghou_3d18j__yanshou", 0)
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d18j__yanshou"] = "雁守",
  [":rfenghou_3d18j__yanshou"] = "①你使用【杀】的次数上限固定为3。<br>" ..
  "②出牌阶段限X次（X为你使用【杀】的剩余次数），你可以<a href='rfenghou_3d18j__yanshou_used_slash'>视为使用过一张【杀】</a>并视为对自己使用【<a href = ':await_exhausted'>以逸待劳</a>】，若你以此法耗尽所有次数，获得此次弃置的牌并令①的数字-1。当你使用耗尽次数的【杀】时，若①的数字为1，此牌不能被响应且伤害+1，然后你复原此技能。",
  [":rfenghou_3d18j__yanshou-dynamic"] = "①你使用【杀】的次数上限固定为{1}。<br>" ..
  "②出牌阶段限{2}次，你可以<a href='rfenghou_3d18j__yanshou_used_slash'>视为使用过一张【杀】</a>并视为对自己使用【<a href = ':await_exhausted'>以逸待劳</a>】，若你以此法耗尽所有次数，获得此次弃置的牌并令①的数字-1。当你使用耗尽次数的【杀】时，若①的数字为1，此牌不能被响应且伤害+1，然后你复原此技能。",
  ["rfenghou_3d18j__yanshou_used_slash"] = "仅增加在【杀】的次数检测中“占用的次数”，特别的，本技能所增加的占用次数不计入〖雁守〗的次数上限计算",

  ["#rfenghou_3d18j__yanshou"] = "雁守：视为对自己使用【以逸待劳】",
  ["#rfenghou_3d18j__yanshou-last"] = "，获得以此法弃置的牌",

  ["@rfenghou_3d18j__yanshou"] = "雁守",
}

return yanshou
